package com.fadis.cmd;

import com.fadis.FadisException;
import com.fadis.expire.ExpireInfo;
import com.fadis.expire.ExpireMgmr;
import com.fadis.protocol.LongNumber;
import com.fadis.db.FadisDB;
import com.fadis.protocol.SimpleString;

public class CmdCommon {

    // ping
    public static final class ping implements CmdHandler{
        @Override
        public String run(FadisActionFrame frame) throws FadisException {
            return SimpleString.encodedSimpleString("PONG");
        }
    }

    // AUTH 123456
    // 密码匹配时返回 OK ，否则返回一个错误
    public static final class auth implements CmdHandler{
        @Override
        public String run(FadisActionFrame frame) throws FadisException {
            String pwd = frame.getArg(1);
            return SimpleString.encodedSimpleString("OK");
        }
    }

    // rename key newkey
    public static final class rename implements CmdHandler{
        @Override
        public String run(FadisActionFrame frame) throws FadisException {
            String key = frame.getArg(1);
            String key2 = frame.getArg(2);
            boolean contains = FadisDB.containsKey(key);
            if(!contains) return LongNumber.encodeInt(0);

            Object data = FadisDB.get(key);
            FadisDB.remove(key);
            FadisDB.remove(key2);
            FadisDB.put(key2, data);

            ExpireInfo e = FadisDB.expireTime.remove(key);
            if(e != null){
                FadisDB.expireTime.put(key2, ExpireMgmr.get(key));
            }

            return SimpleString.encodedSimpleString("OK");
        }
    }

    // flushall
    public static final class flushall implements CmdHandler{
        @Override
        public String run(FadisActionFrame frame) throws FadisException {
            FadisDB.clear();
            return SimpleString.encodedSimpleString("OK");
        }
    }

    // flushdb
    public static final class flushdb implements CmdHandler{
        @Override
        public String run(FadisActionFrame frame) throws FadisException {
            FadisDB.clear();
            return SimpleString.encodedSimpleString("OK");
        }
    }

    // del key [key ...]
    public static final class del implements CmdHandler{
        @Override
        public String run(FadisActionFrame frame) throws FadisException {
            int count = 0;
            for (int i = 1; i < frame.getArgsCount(); i++) {
                boolean contains = FadisDB.containsKey(frame.getArg(i));
                FadisDB.remove(frame.getArg(i));
                if(contains) count++;
            }

            return LongNumber.encodeInt(count);
        }
    }

    // expire key time
    // 成功设置返回1，无返回0
    public static final class expire implements CmdHandler{
        @Override
        public String run(FadisActionFrame frame) throws FadisException {
            String key = frame.getArg(1);
            String timeStr = frame.getArg(2);
            long time = Long.parseLong(timeStr) * 1000;

            boolean r = ExpireMgmr.setExpireTime(key, time);
            return LongNumber.encodeInt(r ? 1 : 0);
        }
    }

    // exists key
    public static final class exists implements CmdHandler{
        @Override
        public String run(FadisActionFrame frame) throws FadisException {
            String key = frame.getArg(1);
           
            boolean contains = FadisDB.containsKey(key);
            return LongNumber.encodeInt(contains ? 1 : 0);
        }
    }

    // ttl key
    // 返回剩余秒数，key不存在返回-2，存在但未设置expire的返回-1
    public static final class ttl implements CmdHandler {
        @Override
        public String run(FadisActionFrame frame) throws FadisException {
            String key = frame.getArg(1);
           
            return LongNumber.encodeLong(ExpireMgmr.ttl(key));
        }
    }

    // ptts
    public static final class pttl implements CmdHandler{
        @Override
        public String run(FadisActionFrame frame) throws FadisException {
            String key = frame.getArg(1);
           
            return LongNumber.encodeLong(ExpireMgmr.pttl(key));
        }
    }

    // PERSIST key
    public static final class persist implements CmdHandler{
        @Override
        public String run(FadisActionFrame frame) throws FadisException {
            String key = frame.getArg(1);
            int r = ExpireMgmr.persist(key);
            return LongNumber.encodeInt(r);
        }
    }

}
